<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\Order;
use App\Models\Base\ServerList;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;

/**
 * 充值排行.
 */
class OrderTopController extends Controller
{
    /**
     * [index 首页].
     *
     * @Author   王文凡
     * @DateTime 2018-04-03
     *
     * @version  1.0
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.order'));
            $content->description(trans('game.order_rank'));
            $content->body($this->grid()->render());
        });
    }

    /**
     * [grid 首页视图渲染].
     *
     * @Author   王文凡
     * @DateTime 2018-04-03
     *
     * @version  1.0
     *
     * @return Grid
     */
    protected function grid()
    {
        ServerList::$soft = true;
        $grid = Admin::grid(Order::class, function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->model()->select(DB::raw('sum(amount) as total_price,player_id,player_name,server_id,channel_id'))
                ->where('status', 1)
                ->groupBy('player_id')
                ->orderBy('total_price', 'desc');
            $grid->player_id(trans('game.role_id'));
            $grid->player_name(trans('game.role_name'));
            $grid->total_price(trans('game.amount'));
            $grid->channel()->name(trans('game.channel'));
            $grid->server()->name(trans('game.server_name'));

            // 字段过滤器
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();

                $filter->equal('server_id', trans('game.server_name'))->select(ServerList::all()->pluck('name', 'server_id'));

                $filter->between('created_at', trans('game.pay_time'))->date();
                $filter->equal('player_name', trans('game.role_name'))->placeholder(trans('game.please_enter_role'));
            });
        });

        return $grid;
    }
}



